<?php
require_once (APPLICATION_DIR."/application/model/Alumni_member_contact.php");
require_once (APPLICATION_DIR."/application/model/Alumni_member_personal.php");
require_once (APPLICATION_DIR."/application/model/Alumni_member_position_current.php");
require_once (APPLICATION_DIR."/application/model/Alumni_member_position_past.php");
require_once (APPLICATION_DIR."/application/model/Alumni_member_recommend.php");
require_once (APPLICATION_DIR."/application/model/Alumni_news.php");
require_once (APPLICATION_DIR."/application/model/Alumni_news_comment.php");
require_once (APPLICATION_DIR."/application/model/Industry.php");
require_once (APPLICATION_DIR."/application/model/Location.php");
require_once (APPLICATION_DIR."/application/model/Alumni_career.php");
require_once (APPLICATION_DIR."/application/model/Alumni_member_intake.php");
class Alumni
{	
	function changeUsername($p_intMemberId, $p_strNewUsername)
	{
		$objMember = new DataObjects_Alumni_member_personal();	
		$objMember -> username = $p_strNewUsername;
		$objMember -> whereAdd("id=" . $p_intMemberId);
		return $objMember -> update(DB_DATAOBJECT_WHEREADD_ONLY);		
	}
	
	function setReceiveNewsletter($p_intMemberId, $p_intValue)
	{
		$objMember = new DataObjects_Alumni_member_personal();	
		$objMember -> receive_newsletter = $p_intValue;
		$objMember -> whereAdd("id=" . $p_intMemberId);
		return $objMember -> update(DB_DATAOBJECT_WHEREADD_ONLY);		
	}
/*====================================================================================================
==============================          CLASSMATE                 =========================================*/
	function incrementRecommendProfile($p_intMemberId)
	{
		$objClassMate = new DataObjects_Alumni_member_personal();	
		$strSql = "UPDATE alumni_member_personal SET recommended=recommended+1 WHERE id=" . $p_intMemberId;
		$objClassMate -> query($strSql);
	}
	function recommendProfile($p_intIdRecommend, $p_intIdRecommender)
	{
		$objClassMate = new DataObjects_Alumni_member_recommend();	
		$objClassMate -> recommend_id = $p_intIdRecommend;
		$objClassMate -> recommender_id = $p_intIdRecommender;
		$objClassMate -> created = date("Y-m-d H:i:s");
		
		return $objClassMate -> insert();
	}
	function checkMemberRecommend($p_intIdRecommend, $p_intIdRecommender)
	{
		$objClassMate = new DataObjects_Alumni_member_recommend();	
		$objClassMate -> selectAdd();
		$objClassMate -> selectAdd("*");
		$objClassMate -> whereAdd("recommend_id=" . $p_intIdRecommend);
		$objClassMate -> whereAdd("recommender_id=" . $p_intIdRecommender);
		
		$arrTmp = $objClassMate -> fetchList();
		if ( count($arrTmp) )
			return true;
		else
			return false;
	}
	function searchClassMate($p_arrInfo, $p_intMemberId = 0)
	{
		$objClassMate = new DataObjects_Alumni_member_personal();	
		$objClassMate -> selectAdd();
		$objClassMate -> selectAdd("alumni_member_personal.*,
																alumni_member_personal.id as mb_id,
																alumni_member_position_current.title as title,
																alumni_member_position_current.company as company,
																alumni_member_position_current.website as website,
																industry.industry_name as industry_name,
																alumni_member_intake.intake_name as intake_name,
																alumni_member_intake.intake_order as intake_order
															");
		
		$objClassMate -> _join = " LEFT JOIN alumni_member_position_current ON alumni_member_position_current.mb_id=alumni_member_personal.id ";
		$objClassMate -> _join .= " LEFT JOIN alumni_member_intake ON alumni_member_intake.id=alumni_member_personal.intake_id ";
		$objClassMate -> _join .= " LEFT JOIN industry ON industry.id=alumni_member_position_current.industry_id ";
		
		if ( trim($p_arrInfo["current_position"]) != "" )
			$objClassMate -> whereAdd("alumni_member_position_current.title LIKE '%" . trim($p_arrInfo["current_position"]) . "%'");
		if ( trim($p_arrInfo["current_company"]) != "" )
			$objClassMate -> whereAdd("alumni_member_position_current.company LIKE '%" . trim($p_arrInfo["current_company"]) . "%'");
		if ( trim($p_arrInfo["industry_id"]) != "-1" )
			$objClassMate -> whereAdd("alumni_member_position_current.industry_id=" . $p_arrInfo["industry_id"]);
		
		if ( trim($p_arrInfo["full_name"]) != "" )
			$objClassMate -> whereAdd("alumni_member_personal.full_name LIKE '%" . trim($p_arrInfo["full_name"]) . "%'");
		if ( trim($p_arrInfo["intake_id"]) != "-1" )
			$objClassMate -> whereAdd("alumni_member_personal.intake_id=" . $p_arrInfo["intake_id"]);
		
		if ( $p_intMemberId )
			$objClassMate -> whereAdd("alumni_member_personal.id <> " . $p_intMemberId);
		
		$objClassMate -> groupBy("alumni_member_personal.id");
		$objClassMate -> limit($p_arrInfo['start'], $p_arrInfo['limit']);
		$objClassMate -> find();
		//echo $objClassMate -> getSelectSql();
		return $objClassMate;
	}

/*====================================================================================================
==============================          CAREER                 =========================================*/
	
	function searchCareer($p_arrInfo)
	{
		$objCareer = new DataObjects_Alumni_career();	
		$objCareer -> selectAdd();
		$objCareer -> selectAdd("alumni_career.*,
														location.location_name as location_name,
														industry.industry_name as industry_name
														");
		$objCareer -> _join = " JOIN location ON location.id=alumni_career.location_id ";
		$objCareer -> _join .= " JOIN industry ON industry.id=alumni_career.industry_id ";
		
		if ( $p_arrInfo['industry_id'] != '-1' )
			$objCareer -> whereAdd("alumni_career.industry_id=" . $p_arrInfo["industry_id"]);
			
		if ( $p_arrInfo['salary_unit'] != '-1' )
		{
			$objCareer -> whereAdd("alumni_career.salary_unit='" . $p_arrInfo["salary_unit"] . "'");
			if ( $p_arrInfo['salary_unit'] == 'vn' )
			{
				if ( $p_arrInfo["salary_from_vn"] != '-1' )
					$objCareer -> whereAdd("alumni_career.salary >= " . $p_arrInfo["salary_from_vn"]);
				if ( $p_arrInfo["salary_to_vn"] != '-1' )
					$objCareer -> whereAdd("alumni_career.salary <= " . $p_arrInfo["salary_to_vn"]);
			}
			elseif ( $p_arrInfo['salary_unit'] == 'usd' )
			{
				if ( $p_arrInfo["salary_from_usd"] != '-1' )
					$objCareer -> whereAdd("alumni_career.salary >= " . $p_arrInfo["salary_from_usd"]);
				if ( $p_arrInfo["salary_to_usd"] != '-1' )
					$objCareer -> whereAdd("alumni_career.salary <= " . $p_arrInfo["salary_to_usd"]);
			}
		}
		
		if ( trim($p_arrInfo['position']) != '' )
			$objCareer -> whereAdd("alumni_career.position LIKE '%" . trim($p_arrInfo["position"]) . "%'");
		
		if ( $p_arrInfo['experience'] != '-1' )
			$objCareer -> whereAdd("alumni_career.experience >= " . $p_arrInfo["experience"]);
		if ( $p_arrInfo['location_id'] != '-1' )
			$objCareer -> whereAdd("alumni_career.location_id = " . $p_arrInfo["location_id"]);
		if ( $p_arrInfo['working_mode'] != '-1' )
			$objCareer -> whereAdd("alumni_career.working_mode = " . $p_arrInfo["working_mode"]);
		$objCareer -> whereAdd("alumni_career.status = 1");
		$objCareer -> limit($p_arrInfo['start'], $p_arrInfo['limit']);
		
		$objCareer -> find();
		return $objCareer;		
	}

/*====================================================================================================
==============================          NEWS                 =========================================*/
	function insertNewsComment($p_arrInfo)
	{
		$objNews = new DataObjects_Alumni_news_comment();	
		$objNews -> aln_news_id = $p_arrInfo['news_id'];
		$objNews -> mb_id = $p_arrInfo['mb_id'];
		$objNews -> content = $p_arrInfo['content'];
		$objNews -> status = 0;
		$objNews -> created = date("Y-m-d H:i:s");
		
		return $objNews -> insert();
	}
	
	function getListNewsOthers($p_intId, $p_intLimit)
	{
		$objNews = new DataObjects_Alumni_news();
		$objNews -> selectAdd();
		$objNews -> selectAdd("*");
		$objNews -> whereAdd("status=1");
		$objNews -> whereAdd("id < " . $p_intId);
		$objNews -> orderBy("created DESC");
		$objNews -> limit(0, $p_intLimit);
		
		return $objNews -> fetchList();
	}
	function getListNewsComment($p_intId)
	{
		$objNews = new DataObjects_Alumni_news_comment();	
		$objNews -> selectAdd();
		$objNews -> selectAdd("alumni_news_comment.*, 
													alumni_member_personal.full_name as full_name,
													alumni_member_personal.avatar as avatar,
													alumni_member_personal.id as mb_id
													");
		$objNews -> _join = " JOIN alumni_member_personal ON alumni_member_personal.id=alumni_news_comment.mb_id ";
		$objNews -> whereAdd("alumni_news_comment.aln_news_id=" . $p_intId);
		//$objNews -> whereAdd("alumni_news_comment.status=1");
		
		return $objNews -> fetchList();
	}
	function getInfoNews($p_intId)
	{
		$objNews = new DataObjects_Alumni_news();
		$objNews -> selectAdd();
		$objNews -> selectAdd("*");
		$objNews -> whereAdd("id=" . $p_intId);
		$objNews -> whereAdd("status=1");
		
		$arrTmp = $objNews -> fetchList();
		if ( count($arrTmp) )
			return $arrTmp[0];
		else
			return false;
	}

	function getListNews($p_intStart, $p_intLimit)
	{
		$objNews = new DataObjects_Alumni_news();
		$objNews -> selectAdd();
		$objNews -> selectAdd("*");
		$objNews -> whereAdd("status=1");
		$objNews -> orderBy("created DESC");
		$objNews -> limit($p_intStart, $p_intLimit);
		
		$objNews -> find();
		return $objNews;
	}

/*=========================================================================================	
===============================        PROFILE       ===========================================*/
	function changePassword($p_intMemberId, $p_strNewPassword)
	{
		$obj = new DataObjects_Alumni_member_personal();
		$obj -> password = md5(trim($p_strNewPassword));
		$obj -> whereAdd("id=" . $p_intMemberId);
		
		return $obj -> update(DB_DATAOBJECT_WHEREADD_ONLY);
	}
	
	function completionProfile($p_strField, $p_intPercent, $p_intMemberId)
	{
		$obj = new DataObjects_Alumni_member_personal();
		$obj -> $p_strField = $p_intPercent;
		$obj -> whereAdd("id=" . $p_intMemberId);
		
		return $obj -> update(DB_DATAOBJECT_WHEREADD_ONLY);
	}
	
	function getListCurrentPosition($p_intMemberId)
	{
		$obj = new DataObjects_Alumni_member_position_current();
		$obj -> selectAdd();
		$obj -> selectAdd("alumni_member_position_current.*,
											industry.industry_name as industry_name
											");
		$obj -> _join = " LEFT JOIN industry ON industry.id=alumni_member_position_current.industry_id ";		
		$obj -> whereAdd("alumni_member_position_current.mb_id=" . $p_intMemberId);	
		$obj -> orderBy("alumni_member_position_current.id ASC");	
		return $obj -> fetchList();
	}
	function addCurrentPosition($p_arrInfo)
	{
		$obj = new DataObjects_Alumni_member_position_current();
		$obj -> mb_id = $p_arrInfo["mb_id"];
		$obj -> title = $p_arrInfo["title"];
		$obj -> company = $p_arrInfo["company"];
		$obj -> website = $p_arrInfo["website"];
		$obj -> industry_id = $p_arrInfo["industry_id"];
		
		return $obj -> insert();
	}
	
	function deleteCurrentPosition($p_intMemberId)
	{
		$obj = new DataObjects_Alumni_member_position_current();
		$obj -> mb_id = $p_intMemberId;
		return $obj -> delete();
	}

	function getListPastPosition($p_intMemberId)
	{
		$obj = new DataObjects_Alumni_member_position_past();
		$obj -> selectAdd();
		$obj -> selectAdd("*");
		$obj -> selectAdd("alumni_member_position_past.*,
											industry.industry_name as industry_name
											");
		$obj -> _join = " LEFT JOIN industry ON industry.id=alumni_member_position_past.industry_id ";		
		$obj -> whereAdd("alumni_member_position_past.mb_id=" . $p_intMemberId);	
		$obj -> orderBy("alumni_member_position_past.id ASC");	
		return $obj -> fetchList();
	}
	
	function addPastPosition($p_arrInfo)
	{
		$obj = new DataObjects_Alumni_member_position_past();
		$obj -> mb_id = $p_arrInfo["mb_id"];
		$obj -> title = $p_arrInfo["title"];
		$obj -> company = $p_arrInfo["company"];
		$obj -> website = $p_arrInfo["website"];
		$obj -> industry_id = $p_arrInfo["industry_id"];
		$obj -> date_from = $p_arrInfo["date_from"];
		$obj -> date_to = $p_arrInfo["date_to"];
		
		return $obj -> insert();
	}
	function deletePastPosition($p_intMemberId)
	{
		$obj = new DataObjects_Alumni_member_position_past();
		$obj -> mb_id = $p_intMemberId;
		return $obj -> delete();
	}
	function changeProfileContact($p_arrInfo, $p_intMemberId)
	{
		$obj = new DataObjects_Alumni_member_contact();
		$obj -> address = $p_arrInfo["address"];
		$obj -> location_id = $p_arrInfo["location_id"];
		$obj -> private_email = $p_arrInfo["private_email"];
		$obj -> business_email = $p_arrInfo["business_email"];
		$obj -> private_tel = $p_arrInfo["private_tel"];
		$obj -> business_tel = $p_arrInfo["business_tel"];
		if ( $p_arrInfo["action"] == "ADD" )
		{
			$obj -> mb_id = $p_intMemberId;
			return $obj -> insert();
		}
		else if ( $p_arrInfo["action"] == "UPDATE" )
		{
			$obj -> whereAdd("mb_id=" . $p_intMemberId);
			return $obj -> update(DB_DATAOBJECT_WHEREADD_ONLY);
		}
	}
	function getInfoMemberContact($p_intId)
	{
		$obj = new DataObjects_Alumni_member_contact();
		$obj -> selectAdd();
		$obj -> selectAdd("alumni_member_contact.*,
											location.location_name as location_name
											");
		$obj -> _join = " LEFT JOIN location ON alumni_member_contact.location_id=location.id ";		
		$obj -> whereAdd("mb_id=" . trim($p_intId));
		
		$arrTmp = $obj -> fetchList();
		if ( count($arrTmp) )
			return $arrTmp[0];
		else
			return false;
	}
	
	function updateProfilePersonal($p_arrInfo)
	{
		$obj = new DataObjects_Alumni_member_personal();
		$obj -> full_name = $p_arrInfo["full_name"];
		$obj -> gender = $p_arrInfo["gender"];
		$obj -> dob = $p_arrInfo['year_dob'] . "-" . $p_arrInfo['month_dob'] . "-" . $p_arrInfo['day_dob'] . " OO:00:00";
		$obj -> pob = $p_arrInfo["pob"];
		$obj -> intake_id = $p_arrInfo["intake_id"];
		$obj -> company_tos = $p_arrInfo["company_tos"];
		$obj -> position_tos = $p_arrInfo["position_tos"];
		
		$obj -> whereAdd("id=" . $p_arrInfo["mb_id"]);
		return $obj -> update(DB_DATAOBJECT_WHEREADD_ONLY);
	}
	function changeAvatar($p_intMemberId, $p_strAvatar)
	{
		$obj = new DataObjects_Alumni_member_personal();
		$obj -> avatar = $p_strAvatar;
		$obj -> whereAdd("id=" . $p_intMemberId);
		return $obj -> update(DB_DATAOBJECT_WHEREADD_ONLY);
	}
	
	function addMemberContactSignUp($p_arrInfo)
	{
		$obj = new DataObjects_Alumni_member_contact();
		$obj -> private_tel = $p_arrInfo["private_tel"];
		$obj -> private_email = $p_arrInfo["private_email"];
		$obj -> mb_id = $p_arrInfo["mb_id"];
		
		return $obj -> insert();
	}
	
	function addMemberPersonal($p_arrInfo)
	{
		$obj = new DataObjects_Alumni_member_personal();
		$obj -> username = $p_arrInfo['username'];
		$obj -> password = md5($p_arrInfo['password']);
		$obj -> full_name = $p_arrInfo['full_name'];
		$obj -> gender = $p_arrInfo['gender'];
		$obj -> intake_id = $p_arrInfo['intake_id'];
		$obj -> receive_newsletter = 1;
		$obj -> status = 0;
		$obj -> dob = $p_arrInfo['year_dob'] . "-" . $p_arrInfo['month_dob'] . "-" . $p_arrInfo['day_dob'] . " OO:00:00";
		$obj -> created = date("Y-m-d H:i:s");
		
		if ( isset($p_arrInfo['company_tos']) )
			$obj -> company_tos = $p_arrInfo['company_tos'];
		if ( isset($p_arrInfo['position_tos']) )
			$obj -> position_tos = $p_arrInfo['position_tos'];
		
		return $obj -> insert();
	}
	
	function getInfoMemberId($p_intId)
	{
		$obj = new DataObjects_Alumni_member_personal();
		$obj -> selectAdd();
		$obj -> selectAdd("alumni_member_personal.*,
											alumni_member_personal.id as mb_id,
											location.location_name as location_name,
											alumni_member_intake.intake_name as intake_name,
											alumni_member_intake.intake_order as intake_order,
											alumni_member_intake.id as intake_id
											");
		$obj -> _join = " LEFT JOIN location ON alumni_member_personal.pob=location.id ";		
		$obj -> _join .= " LEFT JOIN alumni_member_intake ON alumni_member_intake.id=alumni_member_personal.intake_id ";		
		$obj -> whereAdd("alumni_member_personal.id=" . trim($p_intId));
		
		$arrTmp = $obj -> fetchList();
		if ( count($arrTmp) )
			return $arrTmp[0];
		else
			return false;
	}
	function getInfoMemberUsername($p_strUsername)
	{
		$obj = new DataObjects_Alumni_member_personal();
		$obj -> selectAdd();
		$obj -> selectAdd("alumni_member_personal.*,
											alumni_member_personal.id as mb_id,
											location.location_name as location_name,
											alumni_member_intake.intake_name as intake_name,
											alumni_member_intake.intake_order as intake_order,
											alumni_member_intake.id as intake_id
											");
		$obj -> _join = " LEFT JOIN location ON alumni_member_personal.pob=location.id ";	
		$obj -> _join .= " LEFT JOIN alumni_member_intake ON alumni_member_intake.id=alumni_member_personal.intake_id ";			
		$obj -> whereAdd("username='" . trim($p_strUsername) . "'");
		
		$arrTmp = $obj -> fetchList();
		if ( count($arrTmp) )
			return $arrTmp[0];
		else
			return false;
	}
}
?>